﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Web.UI;

using Pampa.CRUD.Core;
using Pampa.CRUD.Exceptions;
using Pampa.CRUD.UI.CustomControls;
using Pampa.CRUD.Configuration.Validations;
using Pampa.WebControls;

namespace Pampa.CRUD.Configuration
{
    public class SelectorItem : EditItem
    {
        int size;
        bool active;

        Selector selector = new Selector();

        public SelectorItem() : base()
        {
            size = 5;
            active = true;

            this.MinimumInputLength = 2;
        }

        public SelectorItem(SelectorItem sourceItem) : base(sourceItem)
        {
            this.Size = sourceItem.Size;
            this.Active = sourceItem.Active;
            this.Multiple = sourceItem.Multiple;
            this.EmptyText = sourceItem.EmptyText;
            this.DataHandler = sourceItem.DataHandler;
            this.FreeText = sourceItem.FreeText;
        }

        #region Properties

        public int Size
        {
            get { return size; }
            set { size = value; }
        }

        public bool Active
        {
            get { return active; }
            set { active = value; }
        }

        public bool Multiple
        {
            get;
            set;
        }

        public string EmptyText
        {
            get;
            set;
        }

        public int MinimumInputLength
        {
            get;
            set;
        }

        public string DataHandler
        {
            get;
            set;
        }

        public bool FreeText
        {
            get;
            set;
        }


        public override ItemType ItemType
        {
            get { return ItemType.Selector; }
        }

        public override Control Control
        {
            get
            {
                selector.Width = this.ValueWidth;
                selector.CssClass = "selector";
                selector.OnClientSelect = this.OnClientChange;
                selector.OnClientUnselect = this.OnClientChange;
                selector.Enabled = this.Enabled;
                selector.Size = this.Size;
                selector.Active = this.Active;
                selector.Multiple = this.Multiple;
                selector.EmptyText = this.EmptyText;
                selector.MinimumInputLength = this.MinimumInputLength;
                selector.FreeText = this.FreeText;
                selector.DataHandler = this.DataHandler;

                if (this.Value != null)
                {
                    selector.Value = GetStringValue();
                    selector.Description = GetStringDescription();
                }

                return selector;
            }
        }

        #endregion

        #region Public Functions

        public override VisibleItem Clone()
        {
            return new SelectorItem(this);
        }

        public override Type ValueTypeDefault
        {
            get { return typeof(System.String); }
        }

        public override void LoadXml(XmlNode node)
        {
            try
            {
                base.LoadXml(node);

                if (node.Attributes["Size"] != null)
                    this.Size = int.Parse(node.Attributes["Size"].Value);

                if (node.Attributes["Active"] != null)
                {
                    string strActive = node.Attributes["Active"].Value;
                    bool isActive;
                    if (bool.TryParse(strActive, out isActive))
                    {
                        Active = isActive;
                    }
                }

                if (node.Attributes["Multiple"] != null)
                {
                    string strMultiple = node.Attributes["Multiple"].Value;
                    bool isMultiple;
                    if (bool.TryParse(strMultiple, out isMultiple))
                    {
                        this.Multiple = isMultiple;
                    }
                }

                if (node.Attributes["EmptyText"] != null)
                    this.EmptyText = node.Attributes["EmptyText"].Value;

                if (node.Attributes["FreeText"] != null)
                {
                    string strFreeText = node.Attributes["FreeText"].Value;
                    bool isFreeText;
                    if (bool.TryParse(strFreeText, out isFreeText))
                    {
                        this.FreeText = isFreeText;
                    }
                }

                if (node.Attributes["DataHandler"] != null)
                    this.DataHandler = node.Attributes["DataHandler"].Value;
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Item " + this.Id + " is not well defined.", ex);
            }
        }

        #endregion

        #region Private functions

        private string GetStringValue()
        {
            if (this.Value is IList)
            {
                var list = (IList)this.Value;
                var values = (from object value in list select GetStringValue(value)).ToList();
                return string.Join(",", values);
            }
            else
            {
                return GetStringValue(this.Value);
            }
        }

        private string GetStringDescription()
        {
            if (this.Value is IList)
            {
                var list = (IList)this.Value;
                var descs = (from object value in list select GetStringDescription(value)).ToList();
                return string.Join(",", descs);
            }
            else
            {
                return GetStringDescription(this.Value);
            }
        }

        private string GetStringValue(object value)
        {
            return value.ToString();
        }

        protected string GetStringDescription(object value)
        {
            return value.ToString();
        }

        #endregion
    }
}
